# -*- coding: utf-8 -*-
"""
Created on Tue Jul 14 09:03:03 2020

@author: Administrator
"""


from PyQt5.QtWidgets import QSizePolicy
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.figure import Figure
import matplotlib.pyplot as plt
import matplotlib.dates as mdates


class MyCanvas(FigureCanvas):
    """FigureCanvas的最终的父类其实是QWidget。"""

    def __init__(self, parent=None, width=5, height=4, dpi=100):

        # 配置中文显示
        plt.rcParams['font.family'] = ['SimHei']  # 用来正常显示中文标签
        plt.rcParams['axes.unicode_minus'] = False  # 用来正常显示负号

        self.fig = Figure(figsize=(width, height), dpi=dpi)  # 新建一个figure
        # self.axes = self.fig.add_subplot(111,projection='3d')  # 建立一个子图，如果要建立复合图，可以在这里修改
        self.axes = self.fig.add_subplot(111)    # 2D图
        
        FigureCanvas.__init__(self, self.fig)
        self.setParent(parent)

        '''定义FigureCanvas的尺寸策略，这部分的意思是设置FigureCanvas，使之尽可能的向外填充空间。'''
        FigureCanvas.setSizePolicy(self,
                                   QSizePolicy.Expanding,
                                   QSizePolicy.Expanding)
        FigureCanvas.updateGeometry(self)

    '''绘制横坐标为时间的静态图，可以在这里定义自己的绘图逻辑'''
    def start_static_plot(self, title, xlabel, ylabel, X, Y):
        self.fig.suptitle(title)
        self.axes.clear()
        
        myFmt = mdates.DateFormatter('%H:%M:%S')
        self.axes.xaxis.set_major_formatter(myFmt)
        self.axes.scatter(X, Y)
        self.axes.axis([X[0],X[-1],min(Y),max(Y)])
        
        self.axes.set_ylabel(ylabel)
        self.axes.set_xlabel(xlabel)
        self.axes.grid(True)
        self.draw()
        # self.fig.savefig(path)
    
    '''绘制横坐标为数字的静态图，可以在这里定义自己的绘图逻辑'''
    def start_number_plot(self, title, xlabel, ylabel, X, Y):
        self.fig.suptitle(title)
        self.axes.clear()
        
        # myFmt = mdates.DateFormatter('%H:%M:%S')
        # self.axes.xaxis.set_major_formatter(myFmt)
        self.axes.scatter(X, Y)
        self.axes.plot([X[0],X[-1]],[Y[0],Y[-1]],'red')
        self.axes.axis([X[0],X[-1],min(Y),max(Y)])
        
        self.axes.set_ylabel(ylabel)
        self.axes.set_xlabel(xlabel)
        self.axes.grid(True)
        self.draw()
        
    '''绘制横坐标为数字的静态图，全局水平航迹图专用'''
    def start_h_trace_plot_global(self, title, xlabel, ylabel, X, Y):
        self.fig.suptitle(title)
        self.axes.clear()
        
        self.axes.scatter(X, Y)
        self.axes.axis([min(X),max(X),min(Y),max(Y)])
        
        self.axes.set_ylabel(ylabel)
        self.axes.set_xlabel(xlabel)
        self.axes.grid(True)
        self.draw()
        
    '''绘制横坐标为数字的静态图，可以在这里定义自己的绘图逻辑'''
    def start_h_trace_plot_local(self, title, xlabel, ylabel, X, Y, OB, OA):
        self.fig.suptitle(title)
        self.axes.clear()
        
        self.axes.scatter(X, Y)
        self.axes.plot([0.0,OB],[0.0,OA],'red')
        # print(X[0],X[-1],Y[0],Y[-1])
        X.append(0)
        Y.append(0)
        X.append(OB)
        Y.append(OA)
        min_X = min(X)
        max_X = max(X)
        min_Y = min(Y)
        max_Y = max(Y)
        self.axes.axis([min_X,max_X,min_Y,max_Y])
        
        self.axes.set_ylabel(ylabel)
        self.axes.set_xlabel(xlabel)
        self.axes.grid(True)
        self.draw()
    
    '''清除绘图'''
    def canvas_clear(self):
        self.fig.suptitle('')
        self.axes.clear()
        self.draw()
        
        